package com.zecan.blog.controller;

import com.zecan.blog.common.BlogResult;
import com.zecan.blog.controller.exception.UserException;
import com.zecan.blog.entity.Classify;
import com.zecan.blog.service.ClassifyService;
import com.zecan.blog.utils.BlogResultUtils;
import com.zecan.blog.vo.article.ArticleMsgVo;
import com.zecan.blog.vo.tag.TagMsgVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ClassifyController
 * @Description
 * @date 2022/11/3 17:08
 * @Version 1.0
 */

@RestController
@RequestMapping(value = "/classify")
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    /**
     * 获取分类信息
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public BlogResult getClassifyAllToList() {
        List<Classify> classifies = classifyService.selectClassifyAll();
        return BlogResultUtils.ok(classifies,"获取分类列表成功");
    }

    /**
     * 根据分类获取文章
     * @param classifyId
     * @param classifyName
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping(value = "/classifyShow/byId",method = RequestMethod.GET)
    public BlogResult getArticleListByClassify(Integer classifyId,String classifyName,Integer offset, Integer limit) {
        List<ArticleMsgVo> articleMsgVos = classifyService.selectArticleListByClassifyId(classifyId,classifyName,offset,limit);
        return  BlogResultUtils.ok(articleMsgVos,"根据id获取分类信息成功");
    }

    @RequestMapping(value = "/listCount",method = RequestMethod.GET)
    public BlogResult getTagListCount() {
        return BlogResultUtils.ok(classifyService.getClassifyListCount(),"获取分类列表数量成功");
    }


    /**
     * 修改分类内容
     * @param classifyId
     * @param newClassifyName
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @RequestMapping(value = "/update_classifyName",method = RequestMethod.GET)
    public BlogResult updateTagName(Integer classifyId,String newClassifyName) {

        if(classifyId == null && newClassifyName == null) {
            return BlogResultUtils.exception(UserException.builder().msg("内容为空").build());
        }

        if(classifyId == null) {
            classifyService.insertClassify(newClassifyName);
            return BlogResultUtils.ok("新增分类成功");
        }else {
            classifyService.updateClassifyName(classifyId, newClassifyName);
            return BlogResultUtils.ok("修改分类信息成功");
        }

    }


    /**
     * 删除标签
     * @param classifyId id
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @RequestMapping(value = "/delete_classify",method = RequestMethod.POST)
    public BlogResult deleteTag(Integer classifyId) {
        classifyService.deleteClassify(classifyId);
        return BlogResultUtils.ok("删除标签成功");
    }

}
